package com._520it.crm.service.impl;

import com._520it.crm.domain.*;
import com._520it.crm.mapper.AuditInfoMapper;
import com._520it.crm.mapper.AuditNotPassMapper;
import com._520it.crm.mapper.OrderBillMapper;
import com._520it.crm.mapper.PaymentHandleMapper;
import com._520it.crm.page.PageResult;
import com._520it.crm.query.AuditInfoQueryObject;
import com._520it.crm.service.IAuditInfoService;
import org.apache.shiro.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;
@Service
public class AuditInfoServiceImpl implements IAuditInfoService {
    @Autowired
    private AuditInfoMapper auditInfoMapper;
    @Autowired
    private OrderBillMapper orderBillMapper;

    @Autowired
    private AuditNotPassMapper auditNotPassMapper;
    @Autowired
    private PaymentHandleMapper paymentHandleMapper;


    @Override
    public int updateByPrimaryKey(AuditInfo record) {

        OrderBill bill = new OrderBill();
        bill.setId(record.getBill().getId());
        bill.setState(OrderBill.STATE_HBYES);
        Employee current = (Employee) SecurityUtils.getSubject().getPrincipal();
        bill.setExamineUserSn(current.getSn());
        orderBillMapper.updateRemark(bill);

        record.setBatchNumber(String.valueOf(System.currentTimeMillis()));


        AuditNotPass anp = new AuditNotPass();
        anp.setAuditTime(new Date());
        anp.setAuditText(record.getOpinion());
        anp.setAuditInfoId(record.getId());
        anp.setEmployeeSn(current.getSn());
        anp.setOrderBillId(record.getBill().getId());
        auditNotPassMapper.insert(anp);

        PaymentHandle paymentHandle = new PaymentHandle();
        paymentHandle.setOrderbillId(bill.getId());
        paymentHandleMapper.insert(paymentHandle);




        return auditInfoMapper.updateByPrimaryKey(record);
    }












    @Override
    public PageResult query(AuditInfoQueryObject qo) {
        Integer count = auditInfoMapper.queryForCount(qo);
        if (count == 0){
            return new PageResult(0, Collections.EMPTY_LIST);
        }
        List<AuditInfo> rows = auditInfoMapper.queryForList(qo);
        return new PageResult(count,rows);
    }

    @Override
    public String queryBatchNumber(Long id) {
        return auditInfoMapper.queryBatchNumber(id);
    }

}
